package Clone01;

import java.util.Arrays;

class Money implements Cloneable{
    public double m = 15.3;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable{
    public String name;
    public int age;
    public Money money = new Money();

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //浅拷贝
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }

    //深拷贝

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student stutclone = (Student) super.clone();
        stutclone.money = (Money) this.money.clone();
        return stutclone;
    }
}

public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student("张三",18);
        Student student2 = (Student) student1.clone();
        System.out.println(student1);
        student1.money.m=10.0;
        System.out.println(student2);
        System.out.println(student1.money.m);
        System.out.println(student2.money.m);
    }
}